package algotithm.leetcode.tree.test98;


class TreeNode {
    TreeNode root;
    TreeNode left;
    TreeNode right;
    int val;
    public TreeNode (int val){
        this.val = val;
    }
}

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/1-11:17
 */

public class Solution {
    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(1);
        TreeNode node3 = new TreeNode(4);
        TreeNode node4 = new TreeNode(3);
        TreeNode node5 = new TreeNode(6);
        node1.left = node2;
        node1.right = node3;
        node2.left = node2.right = null;
        node3.left = node4;
        node3.right = node5;
        node4.left = node4.right = null;
        node5.left = node5.right = null;

        Solution solution = new Solution();
        boolean validBST = solution.isValidBST(node1);
        System.out.println(validBST);
    }

    private long pre = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }

        if (!isValidBST(root.left)) {
            return false;
        }
        if (root.val <= pre) {
            return false;
        }
        pre = root.val;
        return isValidBST(root.right);
    }
}
